package linkedlist;


//                             模拟双向链表
public class MyLinkList {
    //节点
    static public class Node {
        public String val;
        // prve 指向前一个节点
        public Node prve = null;
        public Node next = null;
        public Node(String val){
            this.val = val;
        }
    }
    // head 指向头节点
    public Node head = null;
    public Node tail = null;
    //头插
    public void addFirst(String val){
        Node newNode = new Node(val);
        //特殊情况
        if (head == null){
            head = newNode;
            tail = newNode;
            return;
        }
        head.prve = newNode;
        newNode.next = head;
        head = newNode;
    }
    public void addLast(String val){
        Node newNode = new Node(val);
        //特殊情况
        if (head == null){
            head = newNode;
            tail = newNode;
            return;
        }
        tail.next = newNode;
        newNode.prve = tail;
        tail = newNode;
    }




    public String toString(){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        for (Node cur = head; cur != null; cur = cur.next){
            stringBuilder.append(cur.val);
            if (cur.next != null){
                stringBuilder.append(", ");
            }
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }
}
